package com.linux.huhx.concurreny;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class SemaphoreTest1 {
    public static void main(String[] args) {
        final BusinessTask task = new BusinessTask();
        ExecutorService service = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 3; i++) {
            service.execute(task);
        }
        service.shutdown();
    }

    private static class BusinessTask implements Runnable {
        private int count;
        Lock lock = new ReentrantLock();
        Semaphore semaphore = new Semaphore(1);

        @Override
        public void run() {
            try {
//                semaphore.acquire();
                lock.lock();
                count ++;
                Thread.sleep(1000);
                System.out.println(count);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
//                semaphore.release();
                lock.unlock();
            }
        }
    }
}
